#include "main.h"

GLint windowWidth;
GLint windowHeight;
int location;
GLdouble rotate_offset;
// 缩放倍数
GLdouble x_, y_, z_;
// CG 5
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    // 初始化变量
    initVar();
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(windowWidth, windowHeight);
    glutCreateWindow(PROGRAM_NAME);
    glutDisplayFunc(onRenderScene);
    glutSpecialFunc(onSpecialKeyDown);
    glutKeyboardFunc(onKeyDown);
    glutIdleFunc(idle);
    inital();
    printPromopt();
    glutMainLoop();
    return 0;
}
void printPromopt()
{
    printf("[按键说明]\n"
           "左/右方向键  上/下 一个页面, [h]  查看按键说明\n"
           "在旋转页面按下[空格键]旋转,[s] 查看当前旋转偏移量,[shift+r]复原偏移量\n"
           "缩放页面使用 [x/y/z] 增大xyz方向上的缩放系数,[shift+x/y/z]减小系数,[s]查看系数,[shift+r]复原 \n");
}
void initVar()
{
    // 定义窗体尺寸
    windowWidth = WINDOW_SIZE;
    windowHeight = WINDOW_SIZE;
    rotate_offset = 0;
    x_ = 1.5;
    y_ = 0.5;
    z_ = 1.0;
    location = MAN_FACE;
}

void onRenderScene(void)
{
    // 使用当前颜色清空缓冲区,即设定背景颜色
    glClear(GL_COLOR_BUFFER_BIT);
    // 设定画笔颜色
    // glColor3f(1, 0, 0);
    glColor3f(0, 0, 1);
    switch (location)
    {
    case MAN_FACE:
        fun1_face();
        break;
    case SCALE:
        fun2_scale(x_, y_, z_);
        break;
    case ROTATE:
        fun3_rotate(rotate_offset);
        break;
    case SYMMETRY:
        fun4_symmetry();
        break;
    default:
        break;
    }

    glFlush();
}

void onSpecialKeyDown(GLint key, GLint x, GLint y)
{
    // log_m("Key code %d \n", key);
    switch (key)
    {
    // 下一个
    case GLUT_KEY_RIGHT:
        ++location;
        location %= 4;
        break;
    // 上一个
    case GLUT_KEY_LEFT:
        --location;
        location += 4;
        location %= 4;
        break;
    default:
        break;
    }
    glutPostRedisplay();
}
void onKeyDown(unsigned char key, GLint x, GLint y)
{
    // log_m("Key code %c \n", key);
    switch (key)
    {
    // 跳转到指定的页面
    case '1':
    case '2':
    case '3':
    case '4':
        location = key - '1';
        log_m("跳转到页面%d\n", location + 1);
        break;
    // 开始旋转
    case ' ':
        if (location == ROTATE)
        {
            rotate_offset += 5;
            if (rotate_offset > 360)
            {
                rotate_offset -= 360;
            }
        }
        break;
    case 'x':
        if (location == SCALE)
        {
            x_ += 0.1;
        }
        break;
    case 'X':
        if (location == SCALE)
        {
            x_ -= 0.1;
        }
        break;
    case 'y':
        if (location == SCALE)
        {
            y_ += 0.1;
        }
        break;
    case 'Y':
        if (location == SCALE)
        {
            y_ -= 0.1;
        }
        break;
    case 'z':
        if (location == SCALE)
        {
            z_ += 0.1;
        }
        break;
    case 'Z':
        if (location == SCALE)
        {
            z_ -= 0.1;
        }
        break;
    case 's':
        if (location == SCALE)
        {
            log_m("当前缩放比例为:x=%g,y=%g,z=%g\n", x_, y_, z_);
        }
        else if (location == ROTATE)
        {
            log_m("旋转偏移量为:%g\n", rotate_offset);
        }
        break;
    case 'R':
        log_m("reset\n");
        if (location == SCALE)
        {
            x_ = 1.5;
            y_ = 0.5;
            z_ = 1.0;
        }
        else if (location == ROTATE)
        {
            rotate_offset = 0;
        }
        break;
    case 'h':
        printPromopt();
        break;
    default:
        break;
    }
    glutPostRedisplay();
}

void inital()
{

    gluOrtho2D(-(windowWidth / 2), (windowWidth / 2), -(windowHeight / 2), (windowHeight / 2));

    glClearColor(1, 1, 1, 1);
}
void idle(void) //空闲函数
{
    glutPostRedisplay();
}